﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class LoadAss : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject Prefab;


    void Start()
    {
        //GameObject pre = Instantiate(Prefab);
        //GameObject gra = GameObject.Instantiate(Resources.Load("Attack") as GameObject);
        //AssetBundle ss = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/attack");
        //GameObject ga = GameObject.Instantiate(ss.LoadAsset<GameObject>("Attack 1"));

        //GameObject obj = GameObject.Instantiate(UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>("Assets/GameData/Modle/Attack 1.prefab"));


        //CinfigDataAssets();
        //DesXml();
        //BinaryDatad();
        //DesBinarray();

        TestAssets();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    void DesXml()
    {
        ConfigData con = Deserizable();
        Debug.Log(con.Id + "" + con.Name);
        for(int i =0; i<con.list.Count; i++)
        {
            Debug.Log(con.list[i] + "");
        }
    }

    void DesBinarray()
    {
       ConfigData cc = BinaryDeserializable();
        Debug.Log(cc.Id + "" + cc.Name);
        for (int i = 0; i < cc.list.Count; i++)
        {
            Debug.Log(cc.list[i] + "");
        }
    }
    void CinfigDataAssets()
    {
        ConfigData con = new ConfigData();
        con.Id = 1;
        con.Name = "小小";
        con.list = new List<int>();
        con.list.Add(11);
        con.list.Add(22);
        SerizerableConfig(con);

    }
    /// <summary>
    /// 序列化xml
    /// </summary>
    /// <param name="condata"></param>
    void SerizerableConfig(ConfigData condata)
    {
        FileStream fs = new FileStream(Application.dataPath +"/Config.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
        XmlSerializer xml = new XmlSerializer(condata.GetType());
        xml.Serialize(sw, condata);
        sw.Close();
        fs.Close();

    }
    /// <summary>
    /// 反序列化xml
    /// </summary>
    /// <returns></returns>
    ConfigData Deserizable ()
    {
        FileStream filestream = new FileStream(Application.dataPath + "/Config.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
        XmlSerializer xmls = new XmlSerializer(typeof(ConfigData));
        ConfigData con = (ConfigData)xmls.Deserialize(filestream);
        filestream.Close();
        return con;
    }

    void BinaryDatad()
    {
        ConfigData conf = new ConfigData();
        conf.Id = 6;
        conf.Name ="二进制测试";
        conf.list = new List<int>();
        conf.list.Add(33);
        conf.list.Add(44);
        BinarraySerizeable(conf);


    }
    /// <summary>
    /// 序列化二进制
    /// </summary>
    /// <param name="condata"></param>
    void BinarraySerizeable(ConfigData condata)
    {
        FileStream file = new FileStream(Application.dataPath + "/Test.Bytes", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        BinaryFormatter bin = new BinaryFormatter();
        bin.Serialize(file,condata);
        file.Close();
    }
    /// <summary>
    /// 反序列化二进制
    /// </summary>
    /// <returns></returns>
    ConfigData BinaryDeserializable()
    {
        TextAsset ts = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/Test.Bytes");
        MemoryStream ms = new MemoryStream(ts.bytes);
        BinaryFormatter binfo = new BinaryFormatter();
        ConfigData cond = (ConfigData)binfo.Deserialize(ms);
        ms.Close();
        return cond;
    }

/// <summary>
/// 读取资源
/// </summary>
    void TestAssets()
    {
        AssetsSerizable As = UnityEditor.AssetDatabase.LoadAssetAtPath<AssetsSerizable>("Assets/scripts/CreatTestAssets.asset");

        Debug.Log("ASID:" + As.Id);
        Debug.Log("ASName:" + As.Name);
        foreach(var vv in As.CharacterName)
        {
            Debug.Log("ASList" + vv);
        }
        
    }
}
